package com.woniu.venue.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniu.exception.venueClassroom.VenueClassroomException;
import com.woniu.exception.venueClassroom.VenueClassroomExceptionCode;
import com.woniu.util.StateUtil;
import com.woniu.venue.dto.venueClassroom.VenueClassroomDto;
import com.woniu.venue.model.VenueClassroom;
import com.woniu.venue.dao.VenueClassroomDao;
import com.woniu.venue.param.venueClassroom.AddVenueClassroomParam;
import com.woniu.venue.param.venueClassroom.LoadVenueClassroomParam;
import com.woniu.venue.param.venueClassroom.UpdateVenueClassroomParam;
import com.woniu.venue.service.VenueClassroomService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 杜岷芫
 * @since 2024年07月12日
 */
@Service
public class VenueClassroomServiceImpl extends ServiceImpl<VenueClassroomDao, VenueClassroom> implements VenueClassroomService {
    @Autowired
    private VenueClassroomDao venueClassroomDao;

    @Override
    public VenueClassroomDto add(AddVenueClassroomParam param) {
        QueryWrapper<VenueClassroom> qw = new QueryWrapper<VenueClassroom>().eq("classroom_name", param.getClassroomName());
        VenueClassroom venueClassroom = venueClassroomDao.selectOne(qw);
        if (ObjectUtil.isNotEmpty(venueClassroom)) {
            throw new VenueClassroomException(VenueClassroomExceptionCode.VENUE_CLASSROOM_HAD_EXIST);
        }
        VenueClassroom venueClassroom1 = BeanUtil.toBean(param, VenueClassroom.class);
        venueClassroom1.setClassroomType(JSONUtil.toJsonStr(param.getClassroomTypes()));
        venueClassroomDao.insert(venueClassroom1);
        VenueClassroomDto venueClassroomDto = BeanUtil.toBean(venueClassroom1, VenueClassroomDto.class);
        return venueClassroomDto;
    }

    @Override
    public VenueClassroomDto changeState(Integer classroomId, Integer state) {
        VenueClassroom venueClassroom = venueClassroomDao.selectById(classroomId);
        if (ObjectUtil.isEmpty(venueClassroom)) {
            throw new VenueClassroomException(VenueClassroomExceptionCode.VENUE_CLASSROOM_NOT_EXIST);
        }
        if (venueClassroom.getClassroomState() == state || venueClassroom.getClassroomState() == StateUtil.VENUE_CLASS_ROOM_STATE_DELETE) {
            throw new VenueClassroomException(VenueClassroomExceptionCode.VENUE_CLASSROOM_STATE_ERROR);
        }
        venueClassroom.setClassroomState(state);
        venueClassroomDao.updateById(venueClassroom);
        VenueClassroomDto venueClassroomDto = BeanUtil.toBean(venueClassroom, VenueClassroomDto.class);
        return venueClassroomDto;
    }

    @Override
    public VenueClassroomDto updateVenueClassroom(UpdateVenueClassroomParam param) {
        VenueClassroom venueClassroom = venueClassroomDao.selectById(param.getClassroomId());
        if (ObjectUtil.isEmpty(venueClassroom)) {
            throw new VenueClassroomException(VenueClassroomExceptionCode.VENUE_CLASSROOM_NOT_EXIST);
        }
        if (venueClassroom.getClassroomState() == StateUtil.VENUE_CLASS_ROOM_STATE_DELETE) {
            throw new VenueClassroomException(VenueClassroomExceptionCode.VENUE_CLASSROOM_STATE_ERROR);
        }
        VenueClassroom venueClassroom1 = BeanUtil.toBean(param, VenueClassroom.class);
        venueClassroom1.setClassroomType(JSONUtil.toJsonStr(param.getClassroomTypes()));
        venueClassroom1.setClassroomState(venueClassroom.getClassroomState());
        venueClassroom1.setClassroomId(venueClassroom.getClassroomId());
        venueClassroomDao.updateById(venueClassroom1);
        VenueClassroomDto venueClassroomDto = BeanUtil.toBean(venueClassroom1, VenueClassroomDto.class);
        return venueClassroomDto;
    }

    @Override
    public PageInfo<VenueClassroomDto> load(LoadVenueClassroomParam param) {
        PageHelper.startPage(param.getPageNum() == null ? 1 : param.getPageNum(), param.getPageSize() == null ? 6 : param.getPageSize());
        List<VenueClassroomDto> venueClassroomDtos = venueClassroomDao.load(param);
        PageInfo<VenueClassroomDto> venueClassroomDtoPageInfo = new PageInfo<>(venueClassroomDtos);
        return venueClassroomDtoPageInfo;
    }

    @Override
    public List<VenueClassroom> loadAll(Integer venueId, Integer classType) {
        QueryWrapper<VenueClassroom> qw = new QueryWrapper<VenueClassroom>().eq("venue_id", venueId)
                .eq("classroom_type", classType)
                .eq("classroom_state",1);
        List<VenueClassroom> venueClassrooms = venueClassroomDao.selectList(qw);
        return venueClassrooms;
    }

    @Override
    public List<VenueClassroomDto> getByIds(Set<Integer> ids) {
        List<VenueClassroomDto> venueClassrooms = venueClassroomDao.getByIds(ids);
        return venueClassrooms;
    }
}
